package com.delivery.common;

import org.json.JSONObject;

import android.util.Log;

import com.delivery.module.ConfigModule;
import com.delivery.service.ICategoriesContent;
import com.delivery.service.impl.CategoriesContentService;
import com.delivery.utils.StringUtils;

public class ShareWeibo {
	
	private static String token = "";
	
	private static String tokenSecret = "";
	
	private static ICategoriesContent categoriesContentService = new CategoriesContentService();
	
	/**
	 * 分享图片
	 * @param pathName
	 */
	public static String  shareContent(String pathName){
		getToken();//获取token和tokenSecret
		if(StringUtils.isNotEmpty(token) && StringUtils.isNotEmpty(tokenSecret)){
			StringBuffer params = new StringBuffer(ConfigModule.GET_SHARA_IMG_URL);
			params.append("&token="+token);
			params.append("&tokenSecret="+tokenSecret);
			params.append("&pathName="+pathName);
			try {
				return categoriesContentService.getCategoriesContents(params.toString());
			} catch (Exception e) {
				e.printStackTrace();
				Log.e("params", "分享图片失败");
				return "";
			}
		}
		return "";
		
	}
	/**
	 * 获取令牌
	 */
	public static void getToken(){
		try {
			if(StringUtils.isEmpty(token)){
				String	json = categoriesContentService.getCategoriesContents(ConfigModule.GET_TOKEN_URL);
				System.out.println(json);
				JSONObject	jsonObject = new  JSONObject(json);
				token = jsonObject.getString("token");
				tokenSecret = jsonObject.getString("tokenSecret");
			}
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("sharWieBo", "获取Token失败");
			return;
		}
	}
}
